package main

import (
	"net/http"
	"fmt"
	"html/template"
	"strconv"
	"gitee.com/winnie_gss/MessageBox/logs"
	"net/url"
	"time"
)

//TODO : 首页
func Index(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	sort := r.FormValue("sort")

	if sort == "" {
		sort = "0"
	}

	var data1 JsonData1
	JsonGet(fmt.Sprintf("http://www.51pin.com/article/selectall?sort=%s", sort), &data1)

	tpl, err := template.ParseFiles("view/index.html")
	logs.ErrorLog(err)

	err = tpl.Execute(w, data1)
	logs.ErrorLog(err)
}

//TODO : 创建
func Create(w http.ResponseWriter, r *http.Request) {
	tpl, err := template.ParseFiles("view/create.html")
	logs.ErrorLog(err)

	err = tpl.Execute(w, nil)
	logs.ErrorLog(err)
}

func CreateForm(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	title := r.FormValue("title")
	content := r.FormValue("content")

	var data2 JsonDate2

	JsonPost(fmt.Sprintf("http://www.51pin.com/article/submitcreate"), &data2, url.Values{"title": {title}, "content": {content}})

	if data2.Status {
		http.Redirect(w, r, "/index", 302)
	}
}

//TODO : 编辑
func Editor(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	id, err := strconv.Atoi(r.FormValue("id"))
	logs.ErrorLog(err)

	var data3 JsonDate3
	JsonGet(fmt.Sprintf("http://www.51pin.com/article/editor?id=%d", id), &data3)

	tpl, err := template.ParseFiles("view/editor.html")
	logs.ErrorLog(err)

	err = tpl.Execute(w, data3)
	logs.ErrorLog(err)
}

func EditorForm(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	id := r.FormValue("id")
	title := r.FormValue("title")
	content := r.FormValue("content")

	var data2 JsonDate2
	JsonPost(fmt.Sprintf("http://www.51pin.com/article/submiteditor"), &data2, url.Values{"id": {id}, "title": {title}, "content": {content}})

	if data2.Status {
		http.Redirect(w, r, "/index", 302)
	}
}

//TODO : 删除
func Delete(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	id := r.FormValue("id")

	var data2 JsonDate2
	JsonPost(fmt.Sprintf("http://www.51pin.com/article/delete"), &data2, url.Values{"id": {id}})

	if data2.Status {
		http.Redirect(w, r, "/index", 302)
	}
}

//TODO : 详情
func View(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	id, err := strconv.Atoi(r.FormValue("id"))
	logs.ErrorLog(err)

	var data3 JsonDate3
	JsonGet(fmt.Sprintf("http://www.51pin.com/article/view?id=%d", id), &data3)

	data3.Data.Time2 = time.Unix(data3.Data.Time, 0).Format("2006-01-02 15:04:05")

	tpl, err := template.ParseFiles("view/view.html")
	logs.ErrorLog(err)

	err = tpl.Execute(w, data3)
	logs.ErrorLog(err)
}

func main() {

	logs.InitLog()

	fsh := http.FileServer(http.Dir(GetCurrentDirectory() + "/static"))
	http.Handle("/static/", http.StripPrefix("/static/", fsh))

	//http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	//	log.Println(r.URL.Path)
	//})

	http.HandleFunc("/index", Index)
	http.HandleFunc("/create", Create)
	http.HandleFunc("/createform", CreateForm)
	http.HandleFunc("/editor", Editor)
	http.HandleFunc("/editorform", EditorForm)
	http.HandleFunc("/delete", Delete)
	http.HandleFunc("/view", View)

	//http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	//	http.Redirect(w, r, "/index", 302)
	//})

	err := http.ListenAndServe(":9001", nil)
	logs.ErrorLog(err)
}
